package com.ssy.utils;

import com.ssy.utils.http.EasyHttpClient;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

/**
 * Created by ssy on 2017/11/3.
 */
public class JSUtils {
    static ScriptEngineManager manager = new ScriptEngineManager() ;
    static ScriptEngine engine = manager.getEngineByName("js") ;
    static Logger logger = LoggerFactory.getLogger(JSUtils.class);
    /**
     * 加载js文件
     * @param jsPath
     * @return
     */
    private static FileReader getScript(String jsPath){
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(ResourceUtils.getFile("classpath:"+jsPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return fileReader;
    }
    public static void main(String[] args) throws ScriptException, NoSuchMethodException {
        engine.eval("load('js/utils.js')") ;
        engine.eval("load('js/test.js')") ;
        ((Invocable) engine).invokeFunction("setLog", logger);
        ((Invocable) engine).invokeFunction("setHttpClient",new EasyHttpClient()) ;
        String result = (String) ((Invocable) engine).invokeFunction("login","13125985096","153167");
    }

}
